﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;
using Component;

public partial class BackEnd_NewsModify : System.Web.UI.UserControl
{

    public string nameImage = "";
    protected string onclick = "";
    public string _Url = "";
    public string _idCat = "";
    NewsInfo info;
    public string _parent = "";
    public string _Session = "";
    public int idNew = 0;
    string _UrlCateSubPR = BackEndQuery._AdminQuery + BackEndQuery._NewsL + "&" + ConstString._Parent + ConstString._dauBang;
    string _UrlCateSubPRNews = BackEndQuery._AdminQuery + BackEndQuery._NewsL + "&" + ConstString._idcat + ConstString._dauBang;

    protected void Page_Load(object sender, EventArgs e)
    {        
        
        _idCat = Request.QueryString[ConstString._idcat] ?? "";
        _parent = Request.QueryString[ConstString._Parent] ?? "";
        idNew = ConvertType.ToInt(QueryHelper.GetQueryString(Request, ConstString._idcat));
        if (_idCat != "")
        {
            this.onclick = @"OpenWindowUploadFile('FileManagement/File.aspx?id=ctl11_txtImage&idimg=ctl11_imageUpload','window','width = 630px, height =520px');";
            //this.onclick = @"OpenWindowUploadFile(\'FileManagement/File.aspx?id=ctl11_NewsModify1_txtImage&idimg=ctl11_NewsModify1_imageUpload\',\'window\',\'width = 630px, height =520px\');";

            NewsInfo ninfor = new NewsInfo();
            ninfor = NewsDB.GetById(ConvertType.ToInt(_idCat));
            _Url = "<a href=\"" + _UrlCateSubPR + ConvertType.ToString(ninfor.Parent) + "\">Danh Sách Tin Tức" + "</a>";
            Load_Data();
        }
        if (_idCat == "")
        {
            //this.onclick = @"OpenWindowUploadFile('FileManagement/File.aspx?id=ctl11_NewsModify1_txtImage&idimg=ctl11_NewsModify1_imageUpload','window','width = 630px, height =520px');";
            _Url = "<a href=\"" + _UrlCateSubPR + ConvertType.ToString(_parent) + "\">Danh Sách Tin Tức" + "</a>";
            this.onclick = @"OpenWindowUploadFile('FileManagement/File.aspx?id=ctl11_NewsModify1_txtImage&idimg=ctl11_NewsModify1_imageUpload','window','width = 630px, height =520px');";
            //this.onclick = @"OpenWindowUploadFile(FileManagement/File.aspx?id=" + this.txtImage.ClientID + "&idimg=" + this.imageUpload.ClientID  + ",'window','width = 630px, height =520px');";
        }
  
        //this.onclick = @"OpenWindow(""FileManagement/File.aspx?id=" + this.txtImage.ClientID + @"&idimg=" + this.imageUpload.ClientID + @""",""window"",""width = 630px, height =520px"");";
        //this.onclick = @"OpenWindow(""FileManagement/File.aspx?id=" + this.txtImage.ClientID + @"&idimg=" + this.imageUpload.ClientID + "&" + ConstString._Folder + "=FileUpload" + @""",""window"",""width = 630px, height =520px"");";
        //this.onclick = @"OpenWindowUploadFile(""FileManagement/File.aspx?" + ConstString.NewM + "=" + _idCat.ToString() + "&id=" + this.txtImage.ClientID + @"&idimg=" + this.imageUpload.ClientID + "&" + ConstString._Folder + "=FileUpload" + @""",""window"",""width = 630px, height =520px"");";
        
    }
    private void Load_Data()
    {
        info = NewsDB.GetById(int.Parse(_idCat));
        txtContent.Value = info.Content;
        txtNews.Text = info.News;
        txtSummary.Text = info.Summary;
        txtTitle.Text = info.Titile;
        nameImage = info.Img;
    }

    protected void cmdLuu_Click(object sender, EventArgs e)
    {
        string fileNameImage = this.txtImage.Value;
        if (_idCat == "")
            info = new NewsInfo();
        info.Content = txtContent.Value.Trim();
        info.News = txtNews.Text;        
        info.Summary = txtSummary.Text;
        info.Titile = txtTitle.Text;
        if (fileNameImage.CompareTo("") != 0)
        {
            info.Img = fileNameImage;
        }
        if (_idCat == "")
        {
            info.Parent = _parent == "" ? 0 : int.Parse(_parent);
            if (NewsDB.Insert(info))
                lbMessage.Text = ConstString._themTC;
            else
                lbMessage.Text = ConstString._themTB;
        }
        else
        {
            if (NewsDB.Update(info))
            {
                lbMessage.Text = ConstString._capNhatTC;
            }
            else
                lbMessage.Text = ConstString._capNhatTB;
        }
        Response.Redirect(_UrlCateSubPR+info.Parent.ToString());
    }
}